package com.qinglei.recoup.longconn.service.impl;

import com.qinglei.recoup.common.exception.RecoupException;
import com.qinglei.recoup.common.utils.FileUtils;
import com.qinglei.recoup.longconn.constant.LongConnConstant;
import com.qinglei.recoup.longconn.properties.HealthCareFileStorageProperties;
import com.qinglei.recoup.longconn.service.OssService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class HealthCareFileStorageInit implements InitializingBean {
    private HealthCareFileStorageProperties fileStorageProperties;


    @Value("${dataPath}")
    private String dataFilePath;
    private OssService ossService;
    private LocalTime daySplitTime;
    private LocalTime nightSplitTime;
    private Map<Integer,String>  fileTypeMap = new HashMap<>(2);
    @Autowired(required = false)
    public void setOssService(OssService ossService) {
        this.ossService = ossService;
    }

    public HealthCareFileStorageInit(HealthCareFileStorageProperties fileStorageProperties ) throws RecoupException {
        this.fileStorageProperties = fileStorageProperties;
        fileTypeMap.put(LongConnConstant.FILE_TYPE_USER_DATA, fileStorageProperties.getUserDataDir());
        fileTypeMap.put(LongConnConstant.FILE_TYPE_RAW_DATA, fileStorageProperties.getRawDataDir());
        this.daySplitTime=LocalTime.parse(fileStorageProperties.getDaySplitTime());
        this.nightSplitTime=LocalTime.parse(fileStorageProperties.getNightSplitTime());
        log.info("LocalFileService init complete");

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("HealthCareFileStorageInit  afterPropertiesSet run");
        if (this.ossService != null) {
            log.info("ossService is on");
            List<String> fileList = FileUtils.scanDirectory(this.dataFilePath);
            log.info("find files:{}", fileList);
            fileList.forEach(filePath -> {
                String fileName = filePath.replace(this.dataFilePath, "").replace("\\", "/");
                if (fileName.contains("白天")) {
                    String dateStr = null;
                    for (Map.Entry<Integer, String> entry : fileTypeMap.entrySet()) {
                        int index = fileName.indexOf(entry.getValue());
                        if (index >= 0) {
                            index = index + entry.getValue().length() + 1;
                            dateStr = fileName.substring(index, index + 10);
                            break;
                        }
                    }
                    if (StringUtils.isNotEmpty(dateStr)) {
                        LocalDate date = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                        LocalDateTime dateTime = LocalDateTime.of(date, this.nightSplitTime);
                        if (LocalDateTime.now().isAfter(dateTime)) {
                            ossService.uploadFile(fileName, filePath);
                        }
                    }
                } else if (fileName.contains("晚上")) {
                    String dateStr = null;
                    for (Map.Entry<Integer, String> entry : fileTypeMap.entrySet()) {
                        int index = fileName.indexOf(entry.getValue());
                        if (index >= 0) {
                            index = index + entry.getValue().length() + 1;
                            dateStr = fileName.substring(index, index + 10);
                            break;
                        }
                    }
                    if (StringUtils.isNotEmpty(dateStr)) {
                        LocalDate date = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                        LocalDateTime dateTime = LocalDateTime.of(date.plusDays(1), this.daySplitTime);
                        if (LocalDateTime.now().isAfter(dateTime)) {
                            ossService.uploadFile(fileName, filePath);
                        }
                    }
                }else if (fileName.contains("Daytime")) {
                    String dateStr = null;
                    String[] array = fileName.split("/");
                    dateStr = array[1]+"/"+array[2]+"/"+array[3];
                    if (StringUtils.isNotEmpty(dateStr)) {
                        LocalDate date = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                        LocalDateTime dateTime = LocalDateTime.of(date.plusDays(1), this.nightSplitTime);
                        if (LocalDateTime.now().isAfter(dateTime)) {
                            ossService.uploadFile(fileName, filePath);
                        }
                    }
                }  else if (fileName.contains("Night")) {
                    String dateStr = null;
                    String[] array = fileName.split("/");
                    dateStr = array[1]+"/"+array[2]+"/"+array[3];
                    if (StringUtils.isNotEmpty(dateStr)) {
                        LocalDate date = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                        LocalDateTime dateTime = LocalDateTime.of(date.plusDays(1), this.daySplitTime);
                        if (LocalDateTime.now().isAfter(dateTime)) {
                            ossService.uploadFile(fileName, filePath);
                        }
                    }
                } else {
                    ossService.uploadFile(fileName, filePath);
                }
            });
        }
    }
}
